export const state = () => {
  return {
    allClass: [],
    userClass: []
  }
}
export const getters = {
  aclass: (state) => (classId = 101) => {
    const classification = state.allClass.filter((item) => {
      return item.id === classId
    })[0]
    if (classification) {
      return classification
    }
    return {
      id: 0,
      name: '信息青年',
      map: [{ id: 1, name: '信息青年' }]
    }
  },
  amap: (state, getters) => (classId, mapId) => {
    const map = getters.aclass(classId).map.filter((item) => {
      return item.id === mapId
    })[0]
    if (map) {
      return map
    }
    return {
      id: 0,
      name: '信息青年'
    }
  }
}
export const mutations = {
  setAllClass(state, val) {
    state.allClass = val
  },
  setUserClass(state, val) {
    state.userClass = val
  }
}

export const actions = {
  setAllClass: ({ commit }, allClass) => {
    commit('setAllClass', allClass)
  },
  setUserClass: async ({ commit }, body) => {
    const { status, data } = await body.axios.post(
      '/classification/userClass',
      {
        userClass: body.data
      }
    )
    if (status === 200 && data.code === 0) {
      commit('setUserClass', body.data)
    }
  }
}
